﻿using System.Windows.Input;
using WhiteFlow.DevTrackingTool.UI.Models;
using WhiteFlow.Wpf.Common.Commands;

namespace WhiteFlow.DevTrackingTool.UI.ViewModels
{
    public class ExpensesReportViewModel : WorksheetChildViewModelBase
    {
        #region Constants

        public const string SelectedExpensePropertyName = "SelectedExpense";

        public const string SelectedExpenseIndexPropertyName = "SelectedExpenseIndex";

        #endregion

        #region Fields

        private Expense selectedExpense;

        private int selectedExpenseIndex;

        private ICommand createReportForCurrentDayCommand;

        private ICommand addExpenseCommand;

        private ICommand deleteSelectedExpenseCommand;

        private ICommand clearReportCommand;

        #endregion

        #region Commands

        public ICommand CreateReportForCurrentDayCommand
        {
            get
            {
                if (createReportForCurrentDayCommand == null)
                {
                    createReportForCurrentDayCommand = new DelegateCommand(
                        CreateReportForCurrentDay,
                        CanCreateReportForCurrentDay);
                }

                return createReportForCurrentDayCommand;
            }
        }

        public ICommand AddExpenseCommand
        {
            get
            {
                if (addExpenseCommand == null)
                {
                    addExpenseCommand = new DelegateCommand(
                        AddExpense,
                        CanAddExpense);
                }

                return addExpenseCommand;
            }
        }

        public ICommand DeleteSelectedExpenseCommand
        {
            get
            {
                if (deleteSelectedExpenseCommand == null)
                {
                    deleteSelectedExpenseCommand = new DelegateCommand(
                        DeleteSelectedExpense,
                        CanDeleteSelectedExpense);
                }

                return deleteSelectedExpenseCommand;
            }
        }

        public ICommand ClearReportCommand
        {
            get
            {
                if (clearReportCommand == null)
                {
                    clearReportCommand = new DelegateCommand(
                        ClearReport,
                        CanClearReport);
                }

                return clearReportCommand;
            }
        }

        #endregion

        #region Properties

        public Expense SelectedExpense
        {
            get { return selectedExpense; }
            set
            {
                selectedExpense = value;
                OnPropertyChanged(SelectedExpensePropertyName);
            }
        }

        public int SelectedExpenseIndex
        {
            get { return selectedExpenseIndex; }
            set
            {
                selectedExpenseIndex = value;
                OnPropertyChanged(SelectedExpenseIndexPropertyName);
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpensesReportViewModel"/> class.
        /// </summary>
        /// <param name="ownerWorksheetViewModel">The <see cref="WorksheetViewModel"/> instance that owns this instance.</param>
        public ExpensesReportViewModel(WorksheetViewModel ownerWorksheetViewModel) : base(ownerWorksheetViewModel) { }

        #endregion

        #region Commands Implementations

        private void CreateReportForCurrentDay()
        {
            OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport = new ExpenseReport();

            //TrackingDay trackingDay = new TrackingDay() { CalendarDay = OwnerWorksheetViewModel.SelectedDay };
            //trackingDay.SetOwner(OwnerWorksheetViewModel.Worksheet);

            //int index = 0;
            //if (OwnerWorksheetViewModel.Worksheet.Days.Count > 0)
            //{
            //    while (
            //        index < OwnerWorksheetViewModel.Worksheet.Days.Count &&
            //        trackingDay.CalendarDay.CompareTo(OwnerWorksheetViewModel.Worksheet.Days[index].CalendarDay) > 0)
            //    {
            //        index++;
            //    }
            //}

            //OwnerWorksheetViewModel.Worksheet.Days.Insert(index, trackingDay);
            //OwnerWorksheetViewModel.SelectedTrackingDay = trackingDay;
        }

        private bool CanCreateReportForCurrentDay()
        {
            return (OwnerWorksheetViewModel.SelectedTrackingDay != null);
        }

        private void AddExpense()
        {
            ExpenseReport report = OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport;

            Expense expense = new Expense(report);
            report.Expenses.Add(expense);
        }

        private bool CanAddExpense()
        {
            return (
                OwnerWorksheetViewModel.SelectedTrackingDay != null &&
                OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport != null);
        }

        private void DeleteSelectedExpense()
        {
            int index = OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport.Expenses.IndexOf(SelectedExpense);

            if (index != -1)
            {
                OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport.Expenses.RemoveAt(index);
            }
        }

        private bool CanDeleteSelectedExpense()
        {
            return (SelectedExpense != null);
        }

        private void ClearReport()
        {
            OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport = null;
        }

        private bool CanClearReport()
        {
            return (
                OwnerWorksheetViewModel.SelectedTrackingDay != null &&
                OwnerWorksheetViewModel.SelectedTrackingDay.ExpenseReport != null);
        }

        #endregion
    }
}